/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "ui项滚动视口.h"
#include "UI/UI.h"



static void on_项滚动视口绘制(C_Widget* self, S_2D画布* 画布) {
	C_项滚动视口* view = static_cast<C_项滚动视口*>(self);

	//view->m_背景绘制.m_属性 = f_vg_gen属性(画布);
	//S_凸边图元 图元{};
	//图元.属性 = f_vg_get属性(画布, view->m_背景绘制.m_属性);
	//
	//view->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	//
	//view->m_图标绘制.f_Draw彩色(画布, view->m_背景绘制.m_属性, view->m_图标集);
	//
	//auto& 字体 = f_surface_get字体(*self->m_UIctx, S_UI主题::ui_默认字体);
	//view->m_字符绘制.f_Draw(画布, &字体, view->m_背景绘制.m_属性);

}

static void on_项滚动视口布局(C_Widget* self, C_Widget** ws, uint32 num) {
	if (!num) return;

	C_项滚动视口* view = static_cast<C_项滚动视口*>(self);
	uint32 offset = 0;

	vec2 size = view->m_项大小 + 10;
	vec2 滚动坐标 = view->m_滚动坐标;

	float32 项宽度 = view->m_项大小.x + 10;
	float32 项高度 = view->m_项大小.y + 10;

	int32 列数量 = view->m_Size.x / size.x;
	if (!列数量) return;
	int32 行数量 = view->m_项数据.size() / 列数量;

	f_widget_layout_V滚动钳制(view->m_滚动坐标.y, view->m_Size.y, { 行数量, 列数量 }, 项高度, view->m_项数据.size());


	uint32 y = 0;
	for (; y < 行数量; ++y) {
		for (uint32 x = 0; x < 列数量; ++x) {
			ws[offset]->f_setPos({ x * size.x + 40, y * size.y + 滚动坐标.y });
			++offset;
		}
	}

	行数量 *= 列数量;
	列数量 = view->m_项数据.size() - 行数量;
	for (uint32 x = 0; x < 列数量; ++x) {
		ws[offset]->f_setPos({ x * size.x + 40, y * size.y + 滚动坐标.y });
		++offset;
	}
}

static void on_项滚动视口变换(C_Widget* self) {
	C_项滚动视口* view = static_cast<C_项滚动视口*>(self);

	auto e = self->f_get更新组件();

	//view->m_背景绘制.f_alloc(e.m_Num * 2);
	//view->m_图标绘制.f_alloc(e.m_Num);
	//view->m_字符绘制.f_alloc(e.m_Num * 2, 24);

	for (uint32 i = 0; i < e.m_Num; ++i) {
		C_项面板项* item = static_cast<C_项面板项*>(e.m_W[i]);
		uint32 offset = i * 2;

		vec2 pos = e.m_W[i]->m_Pos;

		//mat3X2 mat;
		//mat.row0 = e.m_W[i]->m_Pos;
		//mat.row1 = e.m_W[i]->m_Size;
		//mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };
		//
		//view->m_背景绘制.f_set单色填充(offset, mat, item->m_颜色A, S_Widget预设凸包图元::m_圆角矩形);
		//
		//float32 icoSize = e.m_W[i]->m_Size.y * 0.5f;
		//mat.row0.x += 2;
		//mat.row0.y += 2;
		//mat.row1 = { icoSize, icoSize };
		//view->m_背景绘制.f_set单色填充(offset + 1, mat, item->m_颜色A + S_RGBA8UI{ 0,0,0,155 }, S_Widget预设凸包图元::m_圆角矩形);
		//
		//mat.row0.x += 2 + icoSize * 0.5;
		//mat.row0.y += 2 + icoSize * 0.5;
		//mat.row1 = { icoSize, icoSize };
		////auto item = view->m_项数据[i];
		//view->m_图标绘制.f_set(item->m_项.m_ico, i, mat, {});
		//
		//
		//mat.row0 = { pos.x + 2, pos.y + e.m_W[i]->m_Size.y - 26 };
		//mat.row1.x = e.m_W[i]->m_Size.x;
		//mat.row1.y = icoSize;
		//view->m_字符绘制.f_绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, offset, item->m_项.m_item.front(), E_对齐方式::e_据中对齐);
		//mat.row0 = { pos.x + icoSize + 4, pos.y };
		//mat.row1 = { e.m_W[i]->m_Size.x - icoSize, e.m_W[i]->m_Size.y * 0.4f };
		//view->m_字符绘制.f_绘制文本({ mat.row0, mat.row1 }, S_UI主题::uic_文字颜色, offset + 1, item->m_项.m_item.back(), E_对齐方式::e_左对齐);
	}

}

static E_事件是否传递 on_滚动视口(C_Widget* self, const S_鼠标& 鼠标) {
	C_项滚动视口* view = static_cast<C_项滚动视口*>(self);

	auto e = self->f_get更新组件();

	view->m_滚动坐标.y += 鼠标.lZ * 30;

	float32 项宽度 = view->m_项大小.x + 10;
	float32 项高度 = view->m_项大小.y + 10;

	int32 列数量 = view->m_Size.x / 项宽度;
	int32 行数量 = view->m_项数据.size() / 列数量;

	f_widget_layout_V滚动钳制(view->m_滚动坐标.y, view->m_Size.y, { 行数量, 列数量 }, 项高度, view->m_项数据.size());

	view->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_继续;
}


static E_事件是否传递 on_项滚动视口_项鼠标进入(C_Widget* self, const S_鼠标& 轮值) {
	C_项滚动视口* box = static_cast<C_项滚动视口*>(self->m_父部件);
	C_项面板项* item = static_cast<C_项面板项*>(self);
	mat3X2 Mat;
	Mat.row0 = self->m_Pos;
	Mat.row1 = self->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };

	//box->m_背景绘制.f_alloc(box->m_项数据.size() * 2);
	//box->m_背景绘制.f_set单色填充(item->m_项.m_ID_X * 2, Mat, S_UI主题::uic_菜单焦点色, S_Widget预设凸包图元::m_圆角矩形);

	return E_事件是否传递::e_事件传递_继续;
}

static E_事件是否传递 on_项滚动视口_项鼠标移出(C_Widget* self, const S_鼠标& 轮值) {
	C_项滚动视口* box = static_cast<C_项滚动视口*>(self->m_父部件);
	C_项面板项* item = static_cast<C_项面板项*>(self);

	mat3X2 Mat;
	Mat.row0 = self->m_Pos;
	Mat.row1 = self->m_Size;
	Mat.row2 = { S_UI主题::ui_面板圆角半径, 0 };

	for (auto& e : box->m_选择项) {
		if(e == item) return E_事件是否传递::e_事件传递_继续;
	}

	//box->m_背景绘制.f_alloc(box->m_项数据.size() * 2 + 1);
	//box->m_背景绘制.f_set单色填充(item->m_项.m_ID_X * 2, Mat, item->m_颜色A, S_Widget预设凸包图元::m_圆角矩形);

	return E_事件是否传递::e_事件传递_继续;
}

static E_事件是否传递 on_项滚动视口_项选择(C_Widget* self, const E_鼠标按钮& 按钮) {
	C_项面板项* item = dynamic_cast<C_项面板项*>(self);
	C_项滚动视口* box = dynamic_cast<C_项滚动视口*>(self->m_父部件);

	for (auto& e : box->m_选择项) {
		e->m_颜色A = { 100,100,100,100 };
	}
	item->m_颜色A = S_UI主题::uic_焦点色;

	box->m_选择项 = {item};
	box->m_更新绘制属性 = true;
	return E_事件是否传递::e_事件传递_终止;
}


C_项滚动视口::C_项滚动视口(S_UI渲染环境& ctx, std::string ico) : C_Widget(&ctx){
	mf_绘制 = on_项滚动视口绘制;
	mf_布局 = on_项滚动视口布局;
	mf_变换 = on_项滚动视口变换;
	mf_鼠标滚轮 = on_滚动视口;

	m_项大小 = { 120, 60 };
	m_滚动坐标 = {};

	m_图标集 = ico;
}

C_项滚动视口::~C_项滚动视口() {
	
}


C_Widget* f_ui_项滚动视口(S_UI渲染环境& ctx, std::string ico) {
	C_项滚动视口* box = new C_项滚动视口(ctx, ico);

	return box;
}


void f_ui_项滚动视口_添加节点项(C_Widget* self, std::u16string name, std::u16string ico, int32 ID) {
	C_项滚动视口* view = static_cast<C_项滚动视口*>(self);

	auto* butItem = new C_项面板项(*self->m_UIctx);
	butItem->f_setSize(view->m_项大小);
	butItem->m_颜色A = { 100,100,100,100 };
	if (ID < 0) {
		ID = view->m_项数据.size();
	}

	butItem->m_项.m_item = { name, u"说明:" };
	butItem->m_项.m_Data = nullptr;
	butItem->m_项.m_ico = ico;
	butItem->m_项.m_ico_color = { 200,200,200,255 };
	butItem->m_项.m_ID_X = ID;
	//butItem->mf_开始拖拽 = on_库项_开始拖拽;
	//butItem->mf_拖拽 = on_库项_拖拽;
	//butItem->mf_结束拖拽 = on_库项_结束拖动;

	butItem->mf_鼠标进入 = on_项滚动视口_项鼠标进入;
	butItem->mf_鼠标离开 = on_项滚动视口_项鼠标移出;
	butItem->mf_鼠标点击 = on_项滚动视口_项选择;
	
	f_widget_push待添加部件(view, butItem, -1, true);

	//S_鼠标双击键位映射* 鼠标双击键位 = f_widget_alloc_鼠标双击键位映射();
	//鼠标双击键位->event = f_NodeView_载入节点;
	//鼠标双击键位->鼠标按键[0] = E_Input_鼠标按键::e_左键;
	//鼠标双击键位->鼠标状态[0] = DEF_按键状态_按下;
	//鼠标双击键位->发生组件 = butItem;
	//f_ui_注册鼠标快捷键(butItem, (S_键位映射*)鼠标双击键位);

	view->m_项数据.push_back({ u"", {}, {name}, {}, (int32)ID , 0, (void*)butItem, false });
}

std::u16string f_ui_项滚动视口_取选择项名称(C_Widget* self) {
	C_项滚动视口& box = *dynamic_cast<C_项滚动视口*>(self);

	if (box.m_选择项.size()) {
		return box.m_选择项.front()->m_项.m_item.at(0);
	}
	return u"";
}








